/**
 * 测试Text类型
 * 理解Text类型的索引 与 Unicode字符 以及 Java char编码单元的区别
 */
package com.tjh.hadoop.writable;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.junit.Test;
/**
 * @author jahen
 * @date 2016年9月11日
 * @time 下午11:41:16
 * @category Hadoop Writable Text
 */

public class TestText {
	
	/**
	 * 测试类似字符串的功能
	 */
	@Test
	public void test1() throws Exception{
		Text txt = new Text("h中elloworld");
		int v = txt.charAt(1);
		System.out.println((char)v);
		int pos = txt.find("o", 3);
		System.out.println(pos);
		System.out.println(txt.getLength());
		// 解码
		System.out.println(Text.decode(txt.getBytes(),0,txt.getLength()));
	}
	
	/**
	 * 串行和反串行
	 */
	@Test
	public void seriaNdeserial() throws Exception{
		long start = System.nanoTime();
		FileOutputStream fos = new FileOutputStream("f:/test2/data.dat");
		DataOutputStream dos = new DataOutputStream(fos);
		//开始串行化
		IntWritable iw = new IntWritable(100);		//4
		iw.write(dos);
		
		LongWritable lw = new LongWritable(200);	//8
		lw.write(dos);
		
		Text txt = new Text("hello");				//5
		txt.write(dos);
		
		txt.set("world");							//5
		txt.write(dos);
		
		iw.set(-10);								//4
		iw.write(dos);
		
		dos.close();
		fos.close();
		System.out.println("duration : " + (System.nanoTime() - start));
		
		//读取文件
		FileInputStream fis = new FileInputStream("f:/test2/data.dat");
		byte[] bytes = new byte[fis.available()];
		fis.read(bytes);
		fis.close();
		
		//反串行
		DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
		iw.readFields(dis);
		System.out.println(iw.get());
		
		lw.readFields(dis);
		System.out.println(lw.get());
		
		//两种读取的方式
//		txt.readFields(dis);
//		System.out.println(txt.toString());
		String string = Text.readString(dis);
		System.out.println(string);
		
		txt.readFields(dis);
		System.out.println(txt.toString());
		
		iw.readFields(dis);
		System.out.println(iw.get());
	}
	
	/**
	 * 上述Writable序列化用了28 Bytes
	 * 
	 * 对比java序列化
	 * java序列化有类型写入，用了172 Bytes
	 * @throws Exception
	 */
	@Test
	public void javaSerilize() throws Exception{
		long start = System.nanoTime();
		FileOutputStream fos = new FileOutputStream("f:/test2/java.data");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(new Integer(100));
		oos.writeObject(new Long(200));
		oos.writeObject(new Long(200));
		oos.writeObject(new String("hello"));
		oos.writeObject(new String("world"));
		oos.writeObject(new Integer(-10));
		oos.close();
		System.out.println(System.nanoTime() - start);
	}
	
	@Test
	public void hadoopSerilize() throws Exception{
		long start = System.nanoTime();
		FileOutputStream fos = new FileOutputStream("d:/hadoop.data");
		DataOutputStream dos = new DataOutputStream(fos);
		new IntWritable(100).write(dos);
		new LongWritable(200).write(dos);
		new Text("hello").write(dos);
		new Text("world").write(dos);
		new IntWritable(-10).write(dos);
		dos.close();
		fos.close();
		System.out.println(System.nanoTime() - start);
	}
}

